import {
  Component, OnInit, ViewEncapsulation, ChangeDetectionStrategy, Input, Output,
  EventEmitter
} from '@angular/core';

@Component({
  selector: 'seed-counter',
  templateUrl: './counter.component.html',
  styleUrls: ['./counter.component.scss'],
  host: {'class': 'seed-counter seed-flexbox'},
  encapsulation: ViewEncapsulation.None,
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class CounterComponent implements OnInit {
  private _min: number;
  private _max: number;
  public quantity = 1;
  @Output() onSelected: EventEmitter<number> = new EventEmitter();

  @Input()
  set min(val) {
    this._min = val;
  }

  get min() {
    return this._min;
  }

  @Input()
  set max(val) {
    this._max = val;
    this.quantity = 1;
  }

  get max() {
    return this._max;
  }

  constructor() {
    this.max = this.min = 1;
  }

  formatNumber(val): number {
    return parseInt(val, 10);
  }

  isValidWithIncrement() {
     // return this.formatNumber(this.quantity) >= this.formatNumber(this.max) ? true : false;
     return this.formatNumber(this.quantity) >= this.formatNumber(this.max);
  }

  isValidWithDecrement() {
    // return this.formatNumber(this.quantity) <= 1 ? true : false;
    return this.formatNumber(this.quantity) <= 1;
  }

  handleDecrement() {
    this.quantity = (this.quantity - 1);
    this.handleChangeQuantity(this.quantity);
  }

  handleIncrement() {
    this.quantity = (this.quantity + 1);
    this.handleChangeQuantity(this.quantity);
  }

  handleChangeQuantity(quantity) {
    if (quantity) {
      this.onSelected.emit(quantity);
    }
  }

  ngOnInit() {
  }

}
